package upc.dew.inscripcionalumnos.dao.impl;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import upc.dew.inscripcionalumnos.dao.AdministradorDAO;
import upc.dew.inscripcionalumnos.exception.ServiceException;
import upc.dew.inscripcionalumnos.model.Requisito;
import org.hibernate.Query;




public class AdministradorDAOImpl extends HibernateDaoSupport implements AdministradorDAO
{
    public AdministradorDAOImpl(SessionFactory sessionFactory)
    {
        setSessionFactory(sessionFactory);
    }

   public List<Requisito> obtenerRequisitos() throws ServiceException
    {
        return getHibernateTemplate().loadAll(Requisito.class);
    }

    public void registrarRequisito(Requisito requisito) throws ServiceException
    {
        Session session = getSessionFactory().getCurrentSession();
        Transaction tx = session.getTransaction();
        tx.begin();
        session.saveOrUpdate(requisito);
        tx.commit();
    }

    public void eliminarRequisito(Integer codigo) throws ServiceException
    {
        Session session = getSessionFactory().getCurrentSession();
        Transaction tx = session.getTransaction();
        session.beginTransaction();
        Requisito requisito = (Requisito)session.load(Requisito.class,
        new Integer(codigo));

        session.delete(requisito);
        tx.commit();

    }

    public void eliminarRequisitoModalidad(Integer requisito) throws ServiceException
    {
        Session session = getSessionFactory().getCurrentSession();
        Transaction tx = session.getTransaction();
        session.beginTransaction();
        Query query = session.getNamedQuery("eliminarRequisitoModalidad");
        query.setInteger("requisito", requisito);
        int rowCount = query.executeUpdate();
        tx.commit();

    }

    public void eliminarModalidad(String modalidad) throws ServiceException
    {
       /**Session session = getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query query = session.getNamedQuery("eliminarRequisitos");
        query.setString("modalidad", modalidad);
        System.out.println(query.getQueryString());
        query.executeUpdate();
        session.close();**/

        Session session = getSessionFactory().getCurrentSession();
        Transaction tx = session.getTransaction();
        session.beginTransaction();
        Query query = session.getNamedQuery("eliminarModalidad");
        query.setString("modalidad", modalidad);
        //query.setString("modalidad", modalidad);
        int rowCount = query.executeUpdate();
        
        tx.commit();
    }

    public void insertarRequisitos(String modalidad, String requisito) throws ServiceException
    {
       Session session = getSessionFactory().getCurrentSession();
       Transaction tx = session.getTransaction();
       session.beginTransaction();
       Query query = session.getNamedQuery("insertarRequisitos");
       query.setString("requisito", requisito);
       query.setString("modalidad", modalidad);
       int rowCount = query.executeUpdate();

       tx.commit();
    }

}